﻿package com.air.swfmessengergui.menu
{
	
import flash.display.NativeWindow;
import flash.display.Stage;
import flash.events.MouseEvent;

import flash.ui.ContextMenu;
import flash.ui.ContextMenuItem;
import flash.display.NativeMenu;
import flash.display.NativeMenuItem;
import flash.events.ContextMenuEvent;

import com.air.swfmessengergui.SWFMessenger_gui;

public class MenuPrincipal extends Object
{
	public var context:NativeMenu;
	
	static private var stage:Stage;
	private var arquivo:NativeMenu;
	
	public function MenuPrincipal(stg:Stage, mn:XML) {
		stage = stg;
		context = new NativeMenu();
		
		//Arquivo
		context.addSubmenu( createSubMenu(mn.submenu[0]), mn.submenu[0].attribute("text") );
		//Visualizar
		context.addSubmenu( createSubMenu(mn.submenu[1]), mn.submenu[1].attribute("text") );
		//Contatos
		context.addSubmenu( createSubMenu(mn.submenu[2]), mn.submenu[2].attribute("text") );
		//Opções
		context.addSubmenu( createSubMenu(mn.submenu[3]), mn.submenu[3].attribute("text") );
		//Ajuda
		context.addSubmenu( createSubMenu(mn.submenu[4]), mn.submenu[4].attribute("text") );
		
		stage.addEventListener(MouseEvent.RIGHT_CLICK , showMenu);
	}
	
	private function showMenu(e:MouseEvent):void {
		context.display( stage, e.stageX, e.stageY);
	}
	
	private function createSubMenu(x:XML):NativeMenu {
		
		var menu:NativeMenu = new NativeMenu();
		var nN:String;
		var n:String;
		var s:Boolean;
		for(var i:int=0; i<x.children().length(); i++) {
			var el:XML = x.children()[i];
			nN = el.localName();
			n = el.attribute("text");
			s = el.attribute("separator")==undefined ? false : true;
			if (nN == "submenu") {//add submenu
				menu.addSubmenu( createSubMenu(el), n );
			} else { //addItem
				var item:NativeMenuItem = new NativeMenuItem( n );
				menu.addItem( item );
				if (s) menu.addItem( new NativeMenuItem( null, true ) );
				
			}
		}
		//context.addSubmenu( menu, nome );
		return menu;
	}
	
	private function createMenuArquivo():void {
		arquivo = new NativeMenu();
		var desconectar:NativeMenuItem = new NativeMenuItem("Desconectar");
		var sair:NativeMenuItem = new NativeMenuItem("Sair");
		
		arquivo.addItem(desconectar);
		arquivo.addItem(sair);
		context.addSubmenu( arquivo, "Arquivo" );
	}
	
	private function doCutCommand(event:ContextMenuEvent):void{trace("cut");}
	private function doCopyCommand(event:ContextMenuEvent):void{trace("copy");}
	private function doPasteCommand(event:ContextMenuEvent):void{trace("paste");}
	
}
}